package com.example.youtube;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import model.Video;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.ResourceBundle;

public class IndexController implements Initializable {
    @FXML
    private GridPane videoGrid;

    private List<Video> videos;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        videos = new ArrayList<>(videoList());
        int col = 0;
        int row = 1;
        try {
            for (Video video : videos) {
                FXMLLoader fxmlLoader = new FXMLLoader();
                fxmlLoader.setLocation(getClass().getResource("thumb.fxml"));
                VBox box = fxmlLoader.load();
                ThumbController thumbController = fxmlLoader.getController();
                thumbController.setData(video);
                if (col == 3) {
                    ++row;
                    col = 0;
                }
                videoGrid.add(box, col++, row);
                GridPane.setMargin(box, new Insets(10));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<Video> videoList() {
        List<Video> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 21; i++) {
            Video video = new Video();
            video.setThumbSrc("img/my_video_"+random.nextInt(1,4)+".png");
            video.setProfileSrc("img/head.png");
            video.setName("视频内容标题1");
            video.setChannel("小胡Pro");
            video.setViews("1000");
            video.setDate("1天前");

            list.add(video);
        }
        return list;
    }
}